//
//  AppDelegate.m
//  花花时尚
//
//  Created by 霍文轩 on 15/8/20.
//  Copyright (c) 22015年 霍文轩. All rights reserved.
//


/**
 * 友盟 KEY: 55ebd5e4e0f55ad3f90017ba
 * 新浪微博：KEY: 3626646372    971ce4258e446f3228668547803fed27
 * QQ QQ空间：ID: 1104770403    KEY: 5kXqlB3YVDHGHHs6
 * 微信 ID: wx1432558e7397dc02  key: 5kXqlB3YVDHGHHs6
 *
 */
#import "AppDelegate.h"
#import "MainViewController.h"
#import "DailyViewController.h"
#import "ProgramaViewController.h"
#import "CollectViewController.h"
#import "SetViewController.h"
#import "FirstLaunchViewController.h"
#import "LeftViewController.h"
#import "PianKeViewController.h"
#import "NightMode.h"
#import "MVViewController.h"


@interface AppDelegate () <FirstLaunchViewControllerDelegate>


@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//    [self setUMeng]; // 友盟的配置
    [self createFile]; // 创建文件夹
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [_window setBackgroundColor: [UIColor blackColor]];
    [_window makeKeyAndVisible];
    [_window release];
    
    [self judgeFirstLaunch]; // 判断是不是第一次启动
    
    return YES;
}
-(void)judgeFirstLaunch{
    //判断是不是第一次启动应用，创建一个文件，如果存在则不是第一次，如果不存在，则是第一次，并创建这个文件
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
    
        FirstLaunchViewController * firstVC = [[FirstLaunchViewController alloc] init];
        [self.window setRootViewController:firstVC];
        firstVC.delegate = self;
        [firstVC release];
    }else{
        self.sliderview = [[YRSideViewController alloc] init];
        LeftViewController *leftviewController = [[LeftViewController alloc] init];
        
        
        self.sliderview.rootViewController = [self createViewControllers];
        self.sliderview.leftViewController = leftviewController;
        self.sliderview.leftViewShowWidth = 200;
        [self.window setRootViewController:self.sliderview];
    }
}


#pragma mark - 引导页完成，按下按钮进入app
-(void)enterApp{
    [self.window setRootViewController:[self createViewControllers]];
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
    whiteView.backgroundColor = [UIColor whiteColor];
    whiteView.alpha = 1;
    [self.window addSubview:whiteView];
    [UIView animateWithDuration:2 animations:^{
        whiteView.alpha = 0;
    } completion:^(BOOL finished) {
        [whiteView release];
    }];
}
#pragma mark - 设置友盟
//-(void)setUMeng{
//    // 设置友盟appKey
//    [UMSocialData setAppKey:@"55ebd5e4e0f55ad3f90017ba"];
//    [UMSocialQQHandler setQQWithAppId:@"1104770403" appKey:@"5kXqlB3YVDHGHHs6" url:@""];    // 集成手机QQ、Qzone开关
//    [UMSocialWechatHandler setWXAppId:@"wx1432558e7397dc02" appSecret:@"41f3273916a96cb00fa2bd9115903e28" url:@""]; // 继承微信好友，朋友圈，收藏开关。如果出错，加入libz类库
//    
//    // 由于苹果审核政策需求，建议对未安装客户端平台进行隐藏，在设置QQ、微信AppID之后调用下面的方法
//    //    [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ,UMShareToQzone,UMShareToWechatSession]];
//}
-(UIViewController *)createViewControllers{
    MainViewController * main = [[MainViewController alloc] init];
    UINavigationController *mNav = [[UINavigationController alloc] initWithRootViewController:main];
    mNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"资讯" image:nil selectedImage:nil];
    
    DailyViewController * daily = [[DailyViewController alloc] init];
    UINavigationController *dNav = [[UINavigationController alloc] initWithRootViewController:daily];
    dNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"每日推荐" image:nil selectedImage:nil];
    
    ProgramaViewController * programa = [[ProgramaViewController alloc] init];
    UINavigationController *prNav = [[UINavigationController alloc] initWithRootViewController:programa];
    prNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"热门栏目" image:nil selectedImage:nil];
    
    CollectViewController * collect = [[CollectViewController alloc] init];
    collect.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"收藏" image:nil selectedImage:nil];
    
    PianKeViewController * pianke = [[PianKeViewController alloc] init];
    UINavigationController * piankeNV = [[UINavigationController alloc] initWithRootViewController:pianke];
    piankeNV.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"阅听" image:nil selectedImage:nil];
    
    MVViewController *mv = [[MVViewController alloc] init];
    UINavigationController *mvNav = [[UINavigationController alloc] initWithRootViewController:mv];
    mvNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"视频" image:nil selectedImage:nil];
    
    self.tabBar = [[UITabBarController alloc] init];
    [_tabBar setViewControllers:@[mNav,dNav,prNav,piankeNV, mvNav]];
    
    
    // tabBar的image不清晰，另外设置imageView显示高清的图片
    CGFloat tabBarCenterYOffset = [UIScreen mainScreen].bounds.size.width / _tabBar.viewControllers.count / 2.0; // 第一个tabbar的中点y坐标
    CGFloat tabBarWidth = [UIScreen mainScreen].bounds.size.width / _tabBar.viewControllers.count;// 每个tabBar的宽度
    for (int i = 0; i < _tabBar.viewControllers.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 0, 30, 30)];
        imageView.center = CGPointMake(tabBarCenterYOffset + tabBarWidth * i, 18); // 根据总viewController的数量来确定imageView的位置
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"item%d.png",i+1]];
        [_tabBar.tabBar addSubview:imageView];
    }
    
    _tabBar.tabBar.barStyle = [NightMode nightModeON]?UIBarStyleBlack:UIBarStyleDefault;
    _tabBar.tabBar.translucent=YES;

    return _tabBar;
}
-(void)createFile{
    
    // 在library为每个视图创建文件夹
    // 在/library/news/recommend
    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@/%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject],@"news",@"recommend"] withIntermediateDirectories:YES attributes:nil error:nil];
    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@/%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject],@"news",@"dress"] withIntermediateDirectories:YES attributes:nil error:nil];
    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@/%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject],@"news",@"beauty"] withIntermediateDirectories:YES attributes:nil error:nil];
    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@/%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject],@"news",@"sport"] withIntermediateDirectories:YES attributes:nil error:nil];
    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@/%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject],@"news",@"play"] withIntermediateDirectories:YES attributes:nil error:nil];
    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@/%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject],@"news",@"food"] withIntermediateDirectories:YES attributes:nil error:nil];
    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@/%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject],@"news",@"read"] withIntermediateDirectories:YES attributes:nil error:nil];
    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@/%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject],@"news",@"travel"] withIntermediateDirectories:YES attributes:nil error:nil];
    
    // /library/daily
    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject],@"daily"] withIntermediateDirectories:YES attributes:nil error:nil];
    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@/%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject],@"daily",@"model"] withIntermediateDirectories:YES attributes:nil error:nil];
    
    // /library/programa
    [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject],@"programa"] withIntermediateDirectories:YES attributes:nil error:nil];
    
    NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject]);
}
@end
